package com.newtouch.cq.base.starter.business.controller;


import com.newtouch.cq.base.starter.business.entity.SysUser;
import com.newtouch.cq.base.starter.business.model.dto.*;
import com.newtouch.cq.base.starter.business.model.dto.*;
import com.newtouch.cq.base.starter.business.model.vo.SysUserPageVo;
import com.newtouch.cq.base.starter.business.model.vo.SysUserVo;
import com.newtouch.cq.base.starter.business.service.IBaseSysUserService;
import com.newtouch.cq.lang.annotation.NoLog;
import com.newtouch.cq.lang.result.MccObjectResult;
import com.newtouch.cq.lang.result.MccPageResult;
import com.newtouch.cq.lang.result.MccResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author Alex
 * @since 2021-08-02
 */
@RestController
@RequestMapping("/sys/users")
@Api(tags = "管理员相关接口")
public class BaseSysUserController {
    @Autowired
    private IBaseSysUserService userService;

    /**
     * 用户新增
     * @param userAddVo
     * @return
     */
    @PostMapping
    @ApiOperation("用户新增")
    @PreAuthorize("hasAuthority('sys_user_add')")
    public MccResult add(@RequestBody SysUserAddDTO userAddVo) {
        userService.add(userAddVo);
        return MccResult.SUCCESS;
    }

    /**
     * 编辑系统管理员
     * @param id
     * @param userUpdDTO
     * @return
     */
    @PutMapping("{id}")
    @ApiOperation("编辑系统管理员")
    @PreAuthorize("hasAuthority('sys_user_upd')")
    @ApiImplicitParam(name = "id", value = "用户标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult update(@PathVariable long id,@RequestBody SysUserUpdDTO userUpdDTO) {
        userService.update(id,userUpdDTO);
        return MccResult.SUCCESS;
    }

    /**
     * 编辑系统管理员个人信息
     * @param dto
     * @return
     */
    @PutMapping("update/ownInfo")
    @ApiOperation("编辑系统管理员个人信息")
    public MccResult updateOwnInfo(@RequestBody SysUserOwnUpdDTO dto) {
        userService.updateOwnInfo(dto);
        return MccResult.SUCCESS;
    }

    /**
     * 修改系统管理员个人密码
     * @param dto
     * @return
     */
    @PutMapping("update/password")
    @ApiOperation("修改系统管理员个人密码")
    public MccResult updatePassword(@RequestBody SysUserPwdUpdDTO dto) {
        userService.updatePassword(dto);
        return MccResult.SUCCESS;
    }

    /**
     * 获取用户详情
     * @param id
     * @return
     */
    @NoLog
    @GetMapping("info/{id}")
    @ApiOperation("获取用户详情")
    @ApiImplicitParam(name = "id", value = "用户标识", dataTypeClass = Long.class,paramType = "path")
    public MccObjectResult<SysUserVo> get(@PathVariable long id) {
        return MccObjectResult.success(userService.getInfo(id));
    }

    /**
     * 获取用户详情
     * @return
     */
    @NoLog
    @GetMapping("getOwnInfo")
    @ApiOperation("获取自己的详细信息")
    public MccObjectResult<SysUserVo> getOwnInfo() {
        return userService.getOwnInfo();
    }

    /**
     * 用户分页查询
     * @param dto
     * @return
     */
    @NoLog
    @GetMapping("pageList")
    @ApiOperation("用户分页查询")
    public MccPageResult<SysUserPageVo> pageList(SysUserPageDTO dto) {
        return userService.pageList(dto);
    }


    /**
     * 角色关联用户分页查询
     * @param roleId
     * @param dto
     * @return
     */
    @NoLog
    @GetMapping("getRoleUserPagedList/{roleId}")
    @ApiOperation("角色关联用户分页查询")
    @ApiImplicitParam(name = "roleId", value = "角色标识", dataTypeClass = Long.class,paramType = "path")
    public MccPageResult<SysUser> getRoleUserPagedList(@PathVariable long roleId, SysUserPageDTO dto) {
        return userService.getRoleUserPagedList(roleId,dto);
    }


    /**
     * 重置密码
     * @param id
     * @return
     */
    @PutMapping("resetPwd/{id}")
    @ApiOperation("重置密码")
    @PreAuthorize("hasAuthority('sys_user_resetpwd')")
    public MccObjectResult<String> resetPwd(@PathVariable long id) {
        return MccObjectResult.success(userService.resetPwd(id));
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('sys_user_del')")
    @ApiImplicitParam(name = "id", value = "用户标识", dataTypeClass = Long.class,paramType = "path")
    public MccResult userDel(@PathVariable long id) {
        userService.userDel(id);
        return MccResult.SUCCESS;
    }


}
